﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace IT_Pro.Forms
{
    public partial class frmMTDanhMucLinhKienPhongIT : DevExpress.XtraEditors.XtraForm
    {
        public frmMTDanhMucLinhKienPhongIT()
        {
            InitializeComponent();
        }

        private void fnc_CreateIndex(DevExpress.XtraGrid.Views.Grid.GridView gridname, DevExpress.XtraGrid.Columns.GridColumn columname)
        {

            for (int i = 0; i < gridname.RowCount; i++)
            {
                gridname.SetRowCellValue(i, columname.FieldName, i + 1);
            }
            gridname.BestFitColumns();
        }
        DataTable dtthemlk = new DataTable();
        DataTable dtITHienCo = new DataTable();
        private void frmMTDanhMucLinhKienPhongIT_Load(object sender, EventArgs e)
        {
            dtthemlk.Columns.Add("STT", Type.GetType("System.String"));
          dtthemlk.Columns.Add("MaLK", Type.GetType("System.String"));
          dtthemlk.Columns.Add("TenLK", Type.GetType("System.String"));
            dtthemlk.Columns.Add("NhaCC", Type.GetType("System.String"));
         dtthemlk.Columns.Add("HanBaoHanh", Type.GetType("System.DateTime"));
           dtthemlk.Columns.Add("TinhTrang", Type.GetType("System.String"));
            loaddlform();
            
        }

        void loaddlform()
        {
            gridItemLinhkien.DataSource = Class.MT_DanhMucLinhKien.HienthidsLK();
            fnc_CreateIndex(gridDetailItemLK, gridColumn4);

            DataTable dttt = Class.MT_MayViTinh.HienthidsDL("MT_TinhTrang");
            cboTinhTrang.Properties.DataSource = dttt;
            cboTinhTrang.Properties.DisplayMember = "TenTT";
            cboTinhTrang.Properties.ValueMember = "IDTT";
            cboTinhTrang.EditValue = dttt.Rows[3]["IDTT"].ToString();

            dateHanBaoHanh.DateTime = DateTime.Now;
            cboNCC.Properties.DataSource = Class.MT_MayViTinh.HienthidsDL("MT_NhaCungCap");
            cboNCC.Properties.DisplayMember = "TenNhaCC";
            cboNCC.Properties.ValueMember = "MaNhaCC";
            dtITHienCo = Class.CV_BanGiaoLinhKien.DanhsachLKPhongIT();
            gridItem.DataSource = dtITHienCo;
            fnc_CreateIndex(gridDetailItem, ct_colum1); 

        }
      
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (cboNCC.EditValue.ToString().Length < 3)
            {
                MessageBox.Show("Bạn chưa chọn nhà cung cấp ");
                return;
            }
            
            int SelectedRow = gridDetailItemLK.FocusedRowHandle;
            if (SelectedRow >= 0)
            {
                DataRow drow = gridDetailItemLK.GetDataRow(SelectedRow);
                string _value = drow["MaLK"].ToString();
                string _name = drow["TenLK"].ToString();
                DataRow dr = dtthemlk.NewRow();
                dr[0] = "0";
                dr[1] = _value;
                dr[2] = _name;
                dr[3] = cboNCC.EditValue.ToString();
               // dr[3] = cboNCC.EditValue.ToString();
                dr[4] = DateTime.Parse(dateHanBaoHanh.Text);
                dr[5] = cboTinhTrang.EditValue.ToString();
                dtthemlk.Rows.Add(dr);

                DataRow drith = dtITHienCo.NewRow();
                drith[0] = "0";
                drith[6] = _value;
                drith[7] = _name;
                drith[4] = cboNCC.EditValue.ToString();
                //drith[3] = cboNCC.EditValue.ToString();
                drith[3] = DateTime.Parse(dateHanBaoHanh.Text);
                drith[5] = cboTinhTrang.EditValue.ToString();
                dtITHienCo.Rows.Add(drith);

                gridItem.DataSource = dtITHienCo;
                fnc_CreateIndex(gridDetailItem, ct_colum1);              
            }
            else
            {
                MessageBox.Show("Chưa chọn linh kiện ");

            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
          if(dtthemlk.Rows.Count>0){
              try
              {
                  Class.CV_BanGiaoLinhKien.CapnhatLinhKienPhongIT(dtthemlk);
                  MessageBox.Show("Cập nhật thành công ");
                  this.Close();
              }
              catch
              {
                  MessageBox.Show("Lỗi - Cập nhật Thất bại ");
              }
          }
            else
          {
              MessageBox.Show(" Bạn chưa thêm linh kiện nào cả ");
          }
        }
    }
}